home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / GMS / Source / Asm / Chunky / 000.CPU1.s next >
Encoding:
Text File  |  1997-05-11  |  12.5 KB  |  501 lines

  1. ;===================================================================================;
  2. ;                                 CHUNKY8 EMULATOR
  3. ;===================================================================================;
  4. ;This c2p routine is also used in rtgmaster, it's not too bad for a CPU only
  5. ;routine.
  6.  
  7. CHUNKYMEM =    MEM_CLEAR    ;<- Fast memory.
  8.  
  9.     INCDIR    "INCLUDES:"
  10.     INCLUDE    "exec/libraries.i"
  11.     INCLUDE    "exec/initializers.i"
  12.     INCLUDE    "exec/resident.i"
  13.     INCLUDE    "exec/exec_lib.i"
  14.     INCLUDE    "hardware/custom.i"
  15.     INCLUDE    "games/games.i"
  16.     INCLUDE    "games/games_lib.i"
  17.     INCLUDE    "games/debug.i"
  18.  
  19. CALL    MACRO
  20.     jsr    _LVO\1(a6)
  21.     ENDM
  22.  
  23. ;===================================================================================;
  24. ;                                  MONITOR DRIVER
  25. ;===================================================================================;
  26.  
  27.     STRUCTURE    ChunkyBase,LIB_SIZE
  28.     ULONG    CHK_SegList
  29.     LABEL    CHKBase_SIZEOF
  30.  
  31.     SECTION    "Chunky8",CODE
  32.  
  33. LibPriority =     0
  34.  
  35. CleanExit:        ;If the user tries to run the library, we
  36.     moveq    #$00,d0    ;don't want to crash...
  37.     rts
  38.  
  39. InitDescrip:
  40.     dc.w    RTC_MATCHWORD    ;UWORD rt_matchword
  41.     dc.l    InitDescrip,EndCode    ;APTR  rt_matchtag, rt_endskip
  42.     dc.b    RTF_AUTOINIT,GMSVersion    ;UBYTE rt_flags, rt_version
  43.     dc.b    NT_LIBRARY,LibPriority    ;UBYTE rt_type, rt_pri
  44.     dc.l    LibName,IDString,Init    ;APTR  rt_name, rt_idstring, rt_init
  45.  
  46. LibName:
  47.     dc.b    "000.CPU1",0
  48.     even
  49.     dc.b    "$VER: "
  50. IDString:
  51.     dc.b    "Chunky 8 Emulator V0.6",10,0
  52.     even
  53.  
  54. Init:    dc.l    CHKBase_SIZEOF,FunctionTable,DataTable,InitRoutine
  55.  
  56. FunctionTable:
  57. FT:    dc.w    -1,Open-FT,Close-FT,Expunge-FT,Null-FT
  58.     dc.w    LIB_emuRemapFunctions-FT
  59.     dc.w    LIB_emuInitRefresh-FT
  60.     dc.w    LIB_emuFreeRefresh-FT
  61.     dc.w    LIB_emuRefreshScreen-FT
  62.     dc.w    -1
  63.  
  64. DataTable:
  65.     INITBYTE LN_TYPE,NT_LIBRARY
  66.     INITLONG LN_NAME,LibName
  67.     INITBYTE LIB_FLAGS,LIBF_SUMUSED!LIBF_CHANGED
  68.     INITWORD LIB_VERSION,GMSVersion
  69.     INITWORD LIB_REVISION,GMSRevision
  70.     INITLONG LIB_IDSTRING,IDString
  71.     dc.l    0
  72.     even
  73.  
  74. ;===================================================================================;
  75. ;                              INITIALISATION ROUTINE
  76. ;===================================================================================;
  77. ;Requires: a0 = SegList
  78. ;       d0 = Library Base.
  79.  
  80. InitRoutine:
  81.     MOVEM.L    D1-D7/A0-A6,-(SP)    ;SP = Save registers.
  82.     move.l    d0,a5    ;a5 = Store pointer to our base.
  83.     move.l    d0,CHKBase    ;ma = Save pointer to our base.
  84.     move.l    a0,CHK_SegList(a5)    ;a5 = Save pointer to SegList.
  85.     move.l    CHKBase(pc),d0    ;d0 = Return library pointer.
  86. .error    MOVEM.L    (SP)+,D1-D7/A0-A6    ;SP = Return registers.
  87.     rts
  88.  
  89. ;===================================================================================;
  90. ;                                   OPEN LIBRARY
  91. ;===================================================================================;
  92.  
  93. Open:    addq.w    #1,LIB_OPENCNT(a6)    ;Increment lib count.
  94.     bclr    #LIBB_DELEXP,LIB_FLAGS(a6)
  95.     move.l    a6,d0    ;Return library base.
  96.     rts
  97.  
  98. ;===================================================================================;
  99. ;                                  CLOSE LIBRARY
  100. ;===================================================================================;
  101.  
  102. Close:    moveq    #$00,d0    ;This is called whenever someone closes
  103.     subq.w    #1,LIB_OPENCNT(a6)    ;our library.
  104.     bne.s    .Exit
  105.     btst    #LIBB_DELEXP,LIB_FLAGS(a6)
  106.     beq.s    .Exit
  107.     bsr.s    Expunge
  108. .Exit    rts
  109.  
  110. ;===================================================================================;
  111. ;                               EXPUNGE THE LIBRARY
  112. ;===================================================================================;
  113.  
  114. Expunge:
  115.     MOVEM.L    D1-D7/A0-A6,-(SP)    ;SP = Save all registers.
  116.     move.l    a6,a5    ;a5 = Our library base.
  117.     tst.w    LIB_OPENCNT(a5)    ;a5 = Do any programs have us open?
  118.     beq.s    .expunge    ;>> = No, so it's safe to expunge ourselves.
  119.     bset    #LIBB_DELEXP,LIB_FLAGS(a5)
  120.     MOVEM.L    (SP)+,D1-D7/A0-A6    ;SP = Return all registers.
  121.     moveq    #$00,d0
  122.     rts
  123.  
  124. .expunge
  125.     move.l    ($4).w,a6    ;a6 = ExecBase.
  126.     move.l    CHK_SegList(a5),d2    ;d2 = Our segment list.
  127.     move.l    a5,a1    ;a1 = Our library base.
  128.     CALL    Remove    ;>> = Remove it.
  129.     move.l    a5,a1    ;a1 = Our library base.
  130.     moveq    #$00,d0    ;d0 = 00
  131.     move.w    LIB_NEGSIZE(a5),d0    ;d0 = LIB_NEGSIZE(base)
  132.     sub.l    d0,a1    ;a1 = (base)-LIB_NEGSIZE
  133.     add.w    LIB_POSSIZE(a5),d0    ;d0 = (LIB_NEGSIZE)+LIB_POSSIZE
  134.     CALL    FreeMem    ;>> = Free the memory.
  135.     move.l    d2,d0    ;d0 = Pointer to segment list.
  136.     MOVEM.L    (SP)+,D1-D7/A0-A6    ;SP = Return all registers, d0 = seglist.
  137.     rts
  138.  
  139. ;===================================================================================;
  140. ;                                    DO NOTHING
  141. ;===================================================================================;
  142.  
  143. Null:    moveq    #$00,d0
  144.     rts
  145.  
  146. ;===================================================================================;
  147. ;                                FUNCTION REMAPPING
  148. ;===================================================================================;
  149. ;Function: Ignore this function, we will only use it to grab the GMSBase.
  150. ;Requires: d0 = GamesBase.
  151. ;       a6 = Chunky Base.
  152. ;Returns:  Nothing.
  153.  
  154. LIB_emuRemapFunctions:
  155.     move.l    d0,GMSBase    ;ma = Save the GMSBase.
  156.     rts
  157.  
  158. ;===================================================================================;
  159. ;                             INITIALISE C2P ALGORITHM
  160. ;===================================================================================;
  161. ;Function: Initialise the C2P algorithm for emuRefreshScreen().  Note how if the
  162. ;       screen is double buffered, we do not allocate a second chunky buffer.
  163. ;       The reason is because a second planar display buffer already exists, so
  164. ;       having a second chunky buffer has no benefit.
  165. ;Requires: a0 = GameScreen
  166. ;       a6 = Chunky Base.
  167. ;Returns:  d0 = ErrorCode.
  168.  
  169. LIB_emuInitRefresh:
  170.     MOVEM.L    D1-D7/A0-A6,-(SP)    ;SP = Return used registers.
  171.     move.l    GMSBase(pc),a6    ;a6 = GMSBase.
  172.  
  173.     ;Move the planar screen displays (allocated in AddScreen()) to
  174.     ;GS_EMemPtrX.
  175.  
  176.     move.l    GS_MemPtr1(a0),GS_EMemPtr1(a0)
  177.     move.l    GS_MemPtr2(a0),GS_EMemPtr2(a0)
  178.     move.l    GS_MemPtr3(a0),GS_EMemPtr3(a0)
  179.  
  180.     ;Allocate the chunky memory, place it in GS_MemPtrX fields and store
  181.     ;the pointers in GS_EFreeX fields to free it later.
  182.  
  183.     move.w    GS_PicWidth(a0),d0    ;d0 = PicWidth
  184.     mulu    GS_PicHeight(a0),d0    ;d0 = (PicWidth)*PicHeight
  185.     moveq    #CHUNKYMEM,d1    ;d1 = Memory Type (see definition).
  186.     CALL    AllocMemBlock    ;>> = Go get the chunky memory.
  187.     move.l    d0,GS_EFree1(a0)    ;a4 = Store chunky buffer for freemem.
  188.     move.l    d0,GS_MemPtr1(a0)    ;a0 = Store chunky buffer #1.
  189.     beq.s    .error    ;>> = Memory allocation error.
  190.  
  191.     move.l    GS_Attrib(a0),d2    ;d2 = Screen attributes.
  192.     and.l    #DBLBUFFER,d2    ;d2 = Double buffer?
  193.     beq.s    .done    ;>> = No, finished.
  194.     move.l    d0,GS_MemPtr2(a0)    ;a0 = Store chunky buffer #1.
  195.  
  196. .done    MOVEM.L    (SP)+,D1-D7/A0-A6    ;SP = Return used registers.
  197.     moveq    #ERR_SUCCESS,d0
  198.     rts
  199.  
  200. .error    MOVEM.L    (SP)+,D1-D7/A0-A6    ;SP = Return used registers.
  201.     moveq    #ERR_FAILED,d0
  202.     rts
  203.  
  204. ;===================================================================================;
  205. ;                            DE-INITIALISE C2P ALGORITHM
  206. ;===================================================================================;
  207. ;Function: Free any allocations we made for the C2P routine.
  208. ;Requires: a0 = GameScreen
  209. ;       a6 = Chunky Base.
  210. ;Returns:  Nothing.
  211.  
  212. LIB_emuFreeRefresh:
  213.     MOVEM.L    D0/A6,-(SP)    ;SP = Save used registers.
  214.     move.l    GMSBase(pc),a6    ;a6 = GMSBase.
  215.     move.l    GS_EFree1(a0),d0    ;d0 = Screen memory 1 (C2P)
  216.     CALL    FreeMemBlock    ;>> = Free screen memory.
  217.     move.l    GS_EFree2(a0),d0    ;d0 = Screen memory 2 (C2P)
  218.     CALL    FreeMemBlock    ;>> = Free screen memory.
  219.     move.l    GS_EFree3(a0),d0    ;d0 = Screen memory 3 (C2P)
  220.     CALL    FreeMemBlock    ;>> = Free screen memory.
  221.     MOVEM.L    (SP)+,D0/A6    ;SP = Return used registers.
  222.     rts
  223.  
  224. ;===================================================================================;
  225. ;                              C2P CONVERSION ROUTINE
  226. ;===================================================================================;
  227. ;Function: Do the C2P process.
  228. ;Requires: a0 = GameScreen
  229. ;       a6 = Chunky Base.
  230. ;Returns:  Nothing.
  231.  
  232. LIB_emuRefreshScreen:
  233.     MOVEM.L    D0-D7/A0-A6,-(SP)    ;SP = Save used registers.
  234.     moveq    #$00,d0    ;d0 = 00.
  235.     moveq    #$00,d1    ;d1 = 00.
  236.     move.w    GS_PicWidth(a0),d0    ;d0 = Width.
  237.     move.w    GS_PicHeight(a0),d1    ;d1 = Height.
  238.     move.l    d0,d2    ;d2 = ByteWidth
  239.     lsr.l    #3,d2    ;d2 = (ByteWidth)/8 [planar]
  240.     move.l    d2,d3    ;d3 = Line modulo.
  241.     mulu    GS_PicHeight(a0),d2    ;d2 = (ByteWidth/8)*Height
  242.  
  243.     move.l    GS_Attrib(a0),d7    ;d7 = GameScreen attributes
  244.     and.l    #DBLBUFFER|TPLBUFFER,d7    ;d7 = Double or triple buffered?
  245.     bne.s    .doubleandtriple    :>> = Yes.
  246. .single    move.l    GS_EMemPtr1(a0),a1    ;a1 = Pointer to planar display.
  247.     move.l    GS_MemPtr1(a0),a0    ;a0 = Pointer to chunky buffer.
  248.     bra.s    .process
  249.  
  250. .doubleandtriple
  251.     move.l    GS_EMemPtr2(a0),a1    ;a1 = Pointer to planar display.
  252.     move.l    GS_MemPtr2(a0),a0    ;a0 = Pointer to chunky buffer.
  253.  
  254.     ;a0 = Chunky buffer.
  255.     ;a1 = First bitplane.
  256.     ;d0 = Width in pixels, multiple of 32.
  257.     ;d1 = Height in pixels (even).
  258.     ;d2 = Bitplane modulo.
  259.     ;d3 = Line modulo. Modulo from start of one line to start of next (linemod)
  260.  
  261. .process
  262.     move.l    d2,a5    ;a5 = Plane Size.
  263.     lsl.l    #3,d2    ;d2 = (PlaneSize)*8
  264.     sub.l    a5,d2    ;d2 = (PlaneSize*8)-PlaneSize
  265.     subq.l    #2,d2    ;d2 = --2
  266.     move.l    d2,a6    ;a6 = (PlaneSize*8-PlaneSize-2)
  267.     lsr.w    #4,d0    ;d0 = Width/32
  268.     ext.l    d0    ;d0 = Make long.
  269.     move.l    d0,d4    ;d4 = Width/32
  270.     subq.l    #1,d4    ;d4 = (Width/32)-1
  271.     move.l    d4,-(SP)    ;SP = Save it.
  272.     add.l    d0,d0    ;d0 = (Width/32)*2
  273.     sub.l    d0,d3    ;d3 = (LineMod)-Width/16
  274.     sub.l    a6,d3    ;d3 =
  275.     move.l    d3,-(SP)    ;SP =
  276.     move.w    d1,d7    ;d7 = Height
  277.     subq.w    #1,d7    ;d7 = (Height)-1 [for loop]
  278.     movea.l    #$f0f0f0f0,a2    ;a2 = $F0F0F0F0
  279.     movea.l    #$cccccccc,a3    ;a3 = $CCCCCCCC
  280.     movea.l    #$aaaa5555,a4    ;a4 = $AAAA5555
  281.     move.l    a2,d6    ;d6 = $F0F0F0F0
  282.     swap    d7    ;d7 = (loop)<<16
  283.     move.w    (6,SP),d7
  284.     move.l    (a0)+,d0
  285.     move.l    (a0)+,d1
  286.     move.l    (a0)+,d2
  287.     move.l    (a0)+,d3
  288.     move.l    d0,d4
  289.     and.l    d6,d0
  290.     eor.l    d0,d4
  291.     lsl.l    #4,d4
  292.     bra    .same
  293.  
  294. .outer    swap    d7
  295.     move.w    (6,sp),d7
  296.     move.w    d5,(a1)
  297.     adda.l    a5,a1
  298.     swap    d5
  299.     move.w    d5,(a1)
  300.     adda.l    (sp),a1
  301.     movem.l    (a0)+,d0-d3
  302.     move.l    d0,d5
  303.     swap    d0
  304.     rol.l    #8,d2
  305.     move.w    d0,d4
  306.     move.b    d2,d4
  307.     swap    d4
  308.     swap    d2
  309.     move.w    d2,d4
  310.     move.b    d0,d4
  311.     ror.w    #8,d4
  312.     move.b    d2,d5
  313.     swap    d5
  314.     swap    d2
  315.     move.w    d2,d5
  316.     swap    d0
  317.     move.b    d0,d5
  318.     ror.w    #8,d5
  319.     move.l    d1,d2
  320.     swap    d1
  321.     rol.l    #8,d3
  322.     move.w    d1,d0
  323.     move.b    d3,d0
  324.     swap    d0
  325.     swap    d3
  326.     move.w    d3,d0
  327.     move.b    d1,d0
  328.     ror.w    #8,d0
  329.     move.b    d3,d2
  330.     swap    d2
  331.     swap    d3
  332.     move.w    d3,d2
  333.     swap    d1
  334.     move.b    d1,d2
  335.     ror.w    #8,d2
  336.     move.l    d2,d3
  337.     move.l    d0,d2
  338.     move.l    d4,d0
  339.     move.l    d5,d1
  340.     move.l    d0,d4
  341.     and.l    d6,d0
  342.     eor.l    d0,d4
  343.     lsl.l    #4,d4
  344.     bra.s    .same
  345.  
  346. .inner    move.w    d5,(a1)
  347.     adda.l    a5,a1
  348.     swap    d5
  349.     move.w    d5,(a1)
  350.     suba.l    a6,a1
  351.     move.l    (a0)+,d0
  352.     move.l    d0,d5
  353.     move.l    (a0)+,d1
  354.     swap    d0
  355.     move.l    (a0)+,d2
  356.     rol.l    #8,d2
  357.     move.w    d0,d4
  358.     move.b    d2,d4
  359.     swap    d4
  360.     swap    d2
  361.     move.w    d2,d4
  362.     move.b    d0,d4
  363.     ror.w    #8,d4
  364.     move.b    d2,d5
  365.     swap    d5
  366.     swap    d2
  367.     move.w    d2,d5
  368.     swap    d0
  369.     move.b    d0,d5
  370.     ror.w    #8,d5
  371.     move.l    d1,d2
  372.     swap    d1
  373.     move.l    (a0)+,d3
  374.     rol.l    #8,d3
  375.     move.w    d1,d0
  376.     move.b    d3,d0
  377.     swap    d0
  378.     swap    d3
  379.     move.w    d3,d0
  380.     move.b    d1,d0
  381.     ror.w    #8,d0
  382.     move.b    d3,d2
  383.     swap    d2
  384.     swap    d3
  385.     move.w    d3,d2
  386.     swap    d1
  387.     move.b    d1,d2
  388.     ror.w    #8,d2
  389.     move.l    d2,d3
  390.     move.l    d0,d2
  391.     move.l    d4,d0
  392.     move.l    d5,d1
  393.     and.l    d6,d0
  394.     eor.l    d0,d4
  395.     lsl.l    #4,d4
  396.  
  397. .same    move.l    d2,d5
  398.     and.l    d6,d5
  399.     eor.l    d5,d2
  400.     lsr.l    #4,d5
  401.     or.l    d5,d0
  402.     or.l    d4,d2
  403.     move.l    d1,d4
  404.     and.l    d6,d1
  405.     eor.l    d1,d4
  406.     move.l    d3,d5
  407.     and.l    d6,d5
  408.     eor.l    d5,d3
  409.     lsr.l    #4,d5
  410.     lsl.l    #4,d4
  411.     or.l    d5,d1
  412.     or.l    d4,d3
  413.     move.l    a3,d6
  414.     move.l    d2,d4
  415.     and.l    d6,d2
  416.     eor.l    d2,d4
  417.     move.l    d3,d5
  418.     and.l    d6,d5
  419.     eor.l    d5,d3
  420.     lsl.l    #2,d4
  421.     or.l    d4,d3
  422.     move.l    a4,d6
  423.     move.l    d3,d4
  424.     and.l    d6,d3
  425.     eor.l    d3,d4
  426.     lsr.w    #1,d4
  427.     swap    d4
  428.     add.w    d4,d4
  429.     or.l    d4,d3
  430.     move.w    d3,(a1)
  431.     adda.l    a5,a1
  432.     lsr.l    #2,d5
  433.     or.l    d5,d2
  434.     move.l    d2,d4
  435.     and.l    d6,d2
  436.     eor.l    d2,d4
  437.     lsr.w    #1,d4
  438.     swap    d3
  439.     move.w    d3,(a1)
  440.     adda.l    a5,a1
  441.     swap    d4
  442.     add.w    d4,d4
  443.     or.l    d4,d2
  444.     move.l    a3,d6
  445.     move.l    d0,d4
  446.     and.l    d6,d0
  447.     eor.l    d0,d4
  448.     move.w    d2,(a1)
  449.     adda.l    a5,a1
  450.     move.l    d1,d5
  451.     and.l    d6,d5
  452.     eor.l    d5,d1
  453.     lsl.l    #2,d4
  454.     or.l    d4,d1
  455.     move.l    a4,d6
  456.     swap    d2
  457.     move.w    d2,(a1)
  458.     adda.l    a5,a1
  459.     move.l    d1,d4
  460.     and.l    d6,d1
  461.     eor.l    d1,d4
  462.     lsr.w    #1,d4
  463.     swap    d4
  464.     add.w    d4,d4
  465.     or.l    d4,d1
  466.     move.w    d1,(a1)
  467.     adda.l    a5,a1
  468.     lsr.l    #2,d5
  469.     or.l    d5,d0
  470.     move.l    d0,d5
  471.     and.l    d6,d0
  472.     eor.l    d0,d5
  473.     swap    d1
  474.     move.w    d1,(a1)
  475.     adda.l    a5,a1
  476.     lsr.w    #1,d5
  477.     swap    d5
  478.     add.w    d5,d5
  479.     or.l    d0,d5
  480.     move.l    a2,d6
  481.     dbra    d7,.inner
  482.     swap    d7
  483.     dbra    d7,.outer
  484.     move.w    d5,(a1)
  485.     adda.l    a5,a1
  486.     swap    d5
  487.     move.w    d5,(a1)
  488.     addq.l    #8,sp
  489.     MOVEM.L    (SP)+,D0-D7/A0-A6    ;SP = Return used registers.
  490.     rts
  491.  
  492. ;===================================================================================;
  493. ;                                       DATA
  494. ;===================================================================================;
  495.  
  496. GMSBase    dc.l    0
  497. CHKBase dc.l    0
  498.  
  499. ;-----------------------------------------------------------------------------------;
  500. EndCode:
  501.